﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace GrindZonesRandomizer
{
    public partial class CreateZoneList : Form
    {

        private readonly Zones _myCustomZoneList = new Zones();
        private Enums.Difficulties _currDiff;

        public CreateZoneList()
        {
            InitializeComponent();
            sfd_ZoneList.Filter = "XML Files|*.xml";
        }

        #region Load/Close

        private void CreateZoneListLoad(object sender, EventArgs e)
        {
            Helpers.A1NZones.ForEach(z => cb_A1Zones.Items.Add(z.ZoneName));
            Helpers.A2NZones.ForEach(z => cb_A2Zones.Items.Add(z.ZoneName));
            Helpers.A3NZones.ForEach(z => cb_A3Zones.Items.Add(z.ZoneName));
        }

        #endregion

        #region Difficulty Changes

        private void CbDifficultySelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_Difficulty.SelectedItem == null) return;

            EnableDisableGroupBoxes(true);
            _currDiff = ((Enums.Difficulties) cb_Difficulty.SelectedIndex + 1);
        }

        #endregion

        #region ButtonEvents

        // Reset Button
        private void BtResetListClick(object sender, EventArgs e)
        {
            _myCustomZoneList.Clear();
            lb_currentZonesList.Items.Clear();

            SetStatus(Color.ForestGreen,
                      string.Format("The list has been cleared"));
        }

        // A1 AddZone Button
        private void BtAddZoneA1Click(object sender, EventArgs e)
        {
            Zone tempZ =
                GetCorrespondingZoneList(1, _currDiff)
                .First(z => z.ZoneName == (string)cb_A1Zones.SelectedItem);

            _myCustomZoneList.Add(tempZ);

            AddZoneToRotation(string.Format("{0} - {1}", tempZ.ZoneName, tempZ.ZoneDifficulty));

            SetStatus(Color.ForestGreen,
                      string.Format("{0} ({1}) Added to Rotation", tempZ.ZoneName, tempZ.ZoneDifficulty));
        }

        // A2 AddZone Button
        private void BtAddZoneA2Click(object sender, EventArgs e)
        {
            Zone tempZ =
                GetCorrespondingZoneList(2, _currDiff)
                .First(z => z.ZoneName == (string)cb_A2Zones.SelectedItem);

            _myCustomZoneList.Add(tempZ);

            AddZoneToRotation(string.Format("{0} - {1}", tempZ.ZoneName, tempZ.ZoneDifficulty));

            SetStatus(Color.ForestGreen,
                      string.Format("{0} ({1}) Added to Rotation", tempZ.ZoneName, tempZ.ZoneDifficulty));
        }

        // A3 AddZone Button
        private void BtAddZoneA3Click(object sender, EventArgs e)
        {
            Zone tempZ =
                GetCorrespondingZoneList(3, _currDiff)
                .First(z => z.ZoneName == (string)cb_A3Zones.SelectedItem);

            _myCustomZoneList.Add(tempZ);

            AddZoneToRotation(string.Format("{0} - {1}", tempZ.ZoneName, tempZ.ZoneDifficulty));

            SetStatus(Color.ForestGreen,
                      string.Format("{0} ({1}) Added to Rotation", tempZ.ZoneName, tempZ.ZoneDifficulty));
        }

        // Delete from Rotation Button
        private void BtDelFromCurrentRotationClick(object sender, EventArgs e)
        {
            if (lb_currentZonesList.SelectedItem == null) return;

            // Get selected item
            int indexToDelete = lb_currentZonesList.SelectedIndex;

            _myCustomZoneList.RemoveAt(indexToDelete);
            lb_currentZonesList.Items.RemoveAt(indexToDelete);

            SetStatus(Color.ForestGreen,"Zone removed to rotation");
        }

        // Save Button
        private void BtSaveZonesToFileClick(object sender, EventArgs e)
        {
            if (sfd_ZoneList.ShowDialog() != DialogResult.OK) return;

            _myCustomZoneList.SaveZones(sfd_ZoneList.FileName);

            SetStatus(Color.ForestGreen,
                      string.Format("{0} saved", sfd_ZoneList.FileName));
        }

        #endregion

        #region Misc

        private void SetStatus(Color stColor, string status)
        {
            lbl_status.ForeColor = stColor;
            lbl_status.Text = status;
        }

        private void EnableDisableGroupBoxes(bool enabledState)
        {
            gb_Act1.Enabled = enabledState;
            gb_Act2.Enabled = enabledState;
            gb_Act3.Enabled = enabledState;
        }

        private void AddZoneToRotation(string z)
        {
            lb_currentZonesList.Items.Add(z);
        }

        private static IEnumerable<Zone> GetCorrespondingZoneList(int act, Enums.Difficulties difficulty)
        {
            switch (difficulty)
            {
                case Enums.Difficulties.Normal:
                    if (act == 1)
                        return Helpers.A1NZones;
                    if (act == 2)
                        return Helpers.A2NZones;
                    if (act == 3)
                        return Helpers.A3NZones;
                    break;
                case Enums.Difficulties.Cruel:
                    if (act == 1)
                        return Helpers.A1CZones;
                    if (act == 2)
                        return Helpers.A2CZones;
                    if (act == 3)
                        return Helpers.A3CZones;
                    break;
                case Enums.Difficulties.Merciless:
                    if (act == 1)
                        return Helpers.A1MZones;
                    if (act == 2)
                        return Helpers.A2MZones;
                    if (act == 3)
                        return Helpers.A3MZones;
                    break;
            }

            return new List<Zone>();
        }

        #endregion

    }
}
